﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LG.Common
{
    public class XmlArgs
    {
        private string _strArgs = string.Empty;
        private bool _isCreate = false;
        private Dictionary<string, string> _args;

        public XmlArgs()
        {
            _args = new Dictionary<string, string>();
        }

        public XmlArgs(string key, object value)
            : this()
        {
            Add(key, value);
        }

        public void Add(string key, object value)
        {
            if (value != null)
            {
                _args.Add(key, value.ToString());
                _isCreate = false;
            }
        }

        public void Remove(string key)
        {
            _args.Remove(key);
            _isCreate = false;
        }

        public void Clear()
        {
            _args.Clear();
            _isCreate = false;
        }

        private string _CreateArgs()
        {
            if (_args.Count == 0)
            {
                return string.Empty;
            }
            StringBuilder sb = new StringBuilder();
            foreach (string key in _args.Keys)
            {
                sb.AppendFormat("<{0}>{1}</{0}>", key, _args[key]);
            }
            return sb.ToString();
        }

        public override string ToString()
        {
            if (!_isCreate)
            {
                _strArgs = _CreateArgs();
            }
            return _strArgs;
        }
    }
}
